package com.taiji.dicom4cloud.smart.rfid.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.taiji.dicom4cloud.common.core.constrant.CommonConstants;
import com.taiji.dicom4cloud.smart.model.entity.Car;
import com.taiji.dicom4cloud.smart.model.entity.RfidEquipment;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.rfid.annoation.HandlerType;
import com.taiji.dicom4cloud.smart.rfid.common.EnumHandler;
import com.taiji.dicom4cloud.smart.rfid.common.MessageHandler;
import com.taiji.dicom4cloud.smart.rfid.config.MqttGatewayService;
import com.taiji.dicom4cloud.smart.rfid.model.RfidMessage;
import com.taiji.dicom4cloud.smart.service.CarService;
import com.taiji.dicom4cloud.smart.service.RfidEquipmentService;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import com.taiji.dicom4cloud.smart.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 接收卡号
 *
 * @Description
 * @Author fanxl
 * @Date 2021/7/30
 **/
@Slf4j
@HandlerType(value = EnumHandler.INVENTORY_DATA)
public class InventoryDataHandler implements MessageHandler {
    @Autowired
    private RfidEquipmentService rfidEquipmentService;
    @Autowired
    private CarService carService;
    @Autowired
    private AlarmRecordService alarmRecordService;
    @Autowired
    private MqttGatewayService mqttGatewayService;
    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public Object handle(RfidMessage message) {
        if (message.getData() == null) {
            return null;
        }
        if(ObjectUtil.isNotEmpty( message.getData())){
            JSONObject invData = (JSONObject)message.getData();
            String carNo = invData.getString("data");

            RfidEquipment equipment = rfidEquipmentService.getByEquipmentId(message.getClientId());
            if(ObjectUtil.isNull(equipment)){
                log.error("INVENTORY_DATA,接收到数据，找不到clientId："+message.getClientId());
                return null;
            }
            if(!equipment.getIsInv().equals(CommonConstants.Status.INV)){
                equipment.setIsInv(CommonConstants.Status.INV);
                rfidEquipmentService.updateById(equipment);
            }

            LocalDateTime time = LocalDateTime.now();

            Map<String,Object> map = sysConfigService.getConfigByKeys("rfid_report_time");
            Integer rfidTime = Integer.parseInt((String)map.get("rfid_report_time"));
            LocalDateTime getTime = time.minusSeconds(rfidTime);
            int count = alarmRecordService.count(Wrappers.<RfidRecord>lambdaQuery()
                    .eq(RfidRecord::getRfidCardNo,carNo)
                    .ge(RfidRecord::getCreateTime, getTime));
            if (count <= 0) {

                Car car = carService.getByRfidNo(carNo);
                RfidRecord record = new RfidRecord();
                BeanUtil.copyProperties(car, record);
                BeanUtil.copyProperties(equipment, record);
                record.setEquipmentName(equipment.getRfidEquipmentName());
                record.setType("0");
                record.setCreateTime(LocalDateTime.now());
                Boolean ret = alarmRecordService.saveRecord(record);
                //设备开启预警参数时，推送预警消息
                if(ret && equipment.getIsAlarm().equals("1")){
                    mqttGatewayService.send2Mqtt("alarm.rfid", JSONObject.toJSONString(record));
                    log.info("发送："+ JSONObject.toJSONString(record));
                }

            }
        }
        return "INVENTORY_DATA";
    }
}
